package Intermediate_algorithm.TreeAndGraph;

import org.junit.Test;

import java.util.LinkedList;
import java.util.Queue;

/*
岛屿数量
给你一个由'1'（陆地）和 '0'（水）组成的的二维网格，请你计算网格中岛屿的数量。
岛屿总是被水包围，并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。
此外，你可以假设该网格的四条边均被水包围。
示例 1：
输入：grid = [
  ["1","1","1","1","0"],
  ["1","1","0","1","0"],
  ["1","1","0","0","0"],
  ["0","0","0","0","0"]
]
输出：1
示例 2：
输入：grid = [
  ["1","1","0","0","0"],
  ["1","1","0","0","0"],
  ["0","0","1","0","0"],
  ["0","0","0","1","1"]
]
输出：3
作者：LeetCode
链接：https://leetcode.cn/leetbook/read/queue-stack/kbcqv/
 */
public class _06岛屿数量 {


    //递归
    //递归可以帮助你完成那些重复的操作
    //考虑好递归退出的出口是实现递归关键
    public int numIslands(char[][] grid) {
        int count = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == '1') {
                    dfs(grid, i, j);
                    count++;
                }
            }
        }
        return count;
    }

    public void dfs(char[][] grid, int row, int col) {
        if (row < 0 || row >= grid.length || col < 0 || col >= grid[0].length || grid[row][col] == '0') {
            return;
        }
        int up = row - 1;
        int down = row + 1;
        int left = col - 1;
        int right = col + 1 ;
        grid[row][col] = '0';
        dfs(grid, row, left);
        dfs(grid, row, right);
        dfs(grid, up, col);
        dfs(grid, down, col);
    }

    //BFS  模拟
    //代码并不需要HashSet进行去重，因为代码中已经提前对四周的'1'更改为'0',等同于进行了HashSet去重
    //如果添加了HashSet去重，会拖慢代码运行速度 -- 20ms
    //8ms
    //O(MN)  O(min(M,N))
    public int numIslands2(char[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int count = 0;
        Queue<int[]> queue = new LinkedList<>();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == '1') {
                    count++;
                    queue.offer(new int[]{i, j});
                    while (!queue.isEmpty()) {
                        int x = queue.peek()[0];
                        int y = queue.peek()[1];
                        int up = x - 1 == -1 ? 0 : x - 1;
                        int down = x + 1 == m ? x : x + 1;
                        int right = y + 1 == n ? y : y + 1;
                        int left = y - 1 == -1 ? 0 : y - 1;
                        grid[x][y] = '0';
                        if (grid[x][right] == '1') {
                            queue.offer(new int[]{x, right});
                            grid[x][right] = '0';  //进一步节省时间
                        }
                        if (grid[x][left] == '1') {
                            queue.offer(new int[]{x, left});
                            grid[x][left] = '0';
                        }
                        if (grid[up][y] == '1') {
                            queue.offer(new int[]{up, y});
                            grid[up][y] = '0';
                        }
                        if (grid[down][y] == '1') {
                            queue.offer(new int[]{down, y});
                            grid[down][y] = '0';
                        }
                        queue.remove();
                    }
                }
            }
        }
        return count;
    }

}
